//外部分享按钮
(function (undefined) {
  var out_share_obj = {}

  //分享配置
  out_share_obj.conf = {
    'sina_t':{'appkey':'1622102288', 'uid':'2105135980'},
    'qq_t':{'appkey':'2105135980'},
    'domain':config.domain.local,
    'site_name':'同城搜搜'
  }

  //分享初始化
  out_share_obj.init = function(param) {    

    if(typeof(param.obj) != 'object') {
        return;
    }

    //如果下级节点存在就直接显示
    next_client = param.obj.nextSibling;
    if(next_client == '[object Text]') {
      next_client = param.obj.nextSibling.nextSibling;
    }


    if(next_client != null && next_client.nodeName == 'SHARE') {
      next_client.style.display = '';      
      return;
    }

    //父级增加浮动样式
    param.obj.parentNode.style.position = 'relative';
    
    //WINOPEN的LEFT和TOP
    out_share_obj.screen_top = (screen.height - 280) / 2;
    out_share_obj.screen_left = (screen.width - 580) / 2;

    //分享参数
    share_param = {
      //来源URL
      href:encodeURIComponent(param.href || document.location.href),
      //标题
      title:encodeURIComponent(param.title || ''),
      //图片
      pic:encodeURIComponent(param.pic || ''),
      //内容简介
      content:encodeURIComponent(param.content || '')
    }

    //分享站点
    share_site_conf = {site:[
	/*{ url:config.domain.local+'/index.php?v=app/share/out_share&url=' + share_param.href + '&title=' + share_param.title + '&content=' + share_param.content,
	  alt:"我的同城",
	  pic:'/plugin/cooperation_unit/share/img/mycity_ico.gif'
	},*/
    {
      url:'http://v.t.sina.com.cn/share/share.php?appkey=1622102288&url=' + share_param.href + '&title=' + share_param.title + '&pic=' + share_param.pic + '&ralateUid=' + out_share_obj.conf.sina_t.uid + '&rnd=' + new Date().getTime(),
      alt:'新浪微博',
      pic:'/plugin/cooperation_unit/share/img/sina_ico.png'
    },{
      url:'http://v.t.qq.com/share/share.php?url=' + share_param.href + '&appkey=' + out_share_obj.conf.qq_t.appkey + '&site=' + out_share_obj.conf.domain + '&pic=' + share_param.pic + '&title=' + share_param.title,
      alt:'腾讯微博',
      pic:'/plugin/cooperation_unit/share/img/qqweibo_logo.png'
    },{
      url:'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?to=pengyou&url=' + share_param.href,
      alt:'朋友',
      pic:'/plugin/cooperation_unit/share/img/py_logo.png'
    },{
      url:'http://sns.qzone.qq.com/cgi-bin/qzshare/cgi_qzshare_onekey?url=' + share_param.href,
      alt:'QQ空间',
      pic:'/plugin/cooperation_unit/share/img/qz_logo.png'
    },{
      url:'http://www.douban.com/recommend/?url=' + share_param.href + '&title=' + share_param.title,
      alt:'豆瓣',
      pic:'/plugin/cooperation_unit/share/img/douban_logo.png'
    },{
      url:'http://share.renren.com/share/buttonshare.do?link=' + share_param.href + '&title=' + share_param.title,
      alt:'人人网',
      pic:'/plugin/cooperation_unit/share/img/renren_ico.png'    
    }]};

    var span_obj = document.createElement('span');

    //循环插入
    for(var i = 0, c = share_site_conf.site.length; i < c; i++) {

      str_obj = out_share_obj.html_group(share_site_conf.site[i].url, share_site_conf.site[i].alt, share_site_conf.site[i].pic);

      //追加到span_obj中
      span_obj.appendChild(str_obj);
    }

    //选择模式
    switch(param.type) {
      //DIV框模式,鼠标移动和离开
      case 1:
        var frame_div = document.createElement('share');
        frame_div.setAttribute('style', 'float:left;position:relative;top:20px;');
        frame_div.innerHTML = '<div class="partake_ico2"><div class="partake2"><div style="float:left;"></div></div></div>';

        frame_div.childNodes[0].childNodes[0].childNodes[0].appendChild(span_obj);

        param.obj.parentNode.appendChild(frame_div);
        
        frame_div.onmouseover = function() {
          this.style.display = '';
        }

        frame_div.onmouseout = function() {
          this.style.display = 'none';
        }

        //鼠标离开事件
        param.obj.onmouseout = function() {
          frame_div.style.display = 'none';
        }
        break;
      //直接植入模式
      default:
        param.obj.appendChild(span_obj);
        break;
    }
  }

  //字符组织
  out_share_obj.html_group = function(url, alt, pic) {
    var html_obj = document.createElement('a');
    html_obj.setAttribute('href', 'javascript:;');
    html_obj.setAttribute('title', alt),
    html_obj.setAttribute('border', 0);

    html_obj.innerHTML = '<img alt="' + alt + '" src="' + config.domain.local + pic + '" />';

    //绑定事件
    html_obj.onclick = function() {
      window.open(url, '同城分享', 'height=600,width=700,top=' + out_share_obj.screen_top + ',left=' + out_share_obj.screen_left + ',toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,status=no');
    }

    return html_obj;
  }  

  //引入样式表
  out_share_obj.add_link = function() {      
    var _style = config.path.plugin + 'share/main.css?v=' + config.version                
    common_fun_obj.append_style(_style);    
  }

  if(window.out_share_obj == undefined) {
     window.out_share_obj = out_share_obj;
     out_share_obj.add_link();
  }
})();
